package com.blueocean_health.care.management.transfer.server;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.blueocean_health.care.management.common.base.em.TimeType;
import com.blueocean_health.care.management.common.base.time.TimeUtils;
import com.blueocean_health.care.management.common.excel.ExcelUtil;
import com.blueocean_health.care.management.domain.base.SysResult;
import com.blueocean_health.care.management.domain.dto.OrderInfoDto;
import com.blueocean_health.care.management.domain.dto.OrderSelectInfoEntity;
import com.blueocean_health.care.management.domain.dto.RelationListDto;
import com.blueocean_health.care.management.domain.ordervo.SelectOrderVo;
import com.blueocean_health.care.management.service.IOrderSelectService;


@RestController
@RequestMapping(value = "order")
public class SelectOrderSever{
	@Resource
	private IOrderSelectService iOrderSelectService;
	@RequestMapping(value = "list")
	//查询订单列表
	public SysResult selectOrderList(@RequestBody(required = false) SelectOrderVo order){
		//正常订单已编辑过
		//order.setOrderUpdateFlag(OrderUpdateFlagEnum._1.getKey());
		order.setHospitalId(order.getTeamId());
		Map<String, Object> selectOrderList = iOrderSelectService.selectOrderList(order);
		
		return SysResult.ok(selectOrderList);
		
	}
	@RequestMapping(value = "relationList")
	//查询关联订单列表
	public SysResult relationList(@RequestBody(required = false) SelectOrderVo order){
		List<RelationListDto> relationList = iOrderSelectService.relationList(order);
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("list", relationList);
		return SysResult.ok(map);
		
	}
	@RequestMapping(value = "listExcel")
	//查询订单列表
		public void selectOrderListExcel(SelectOrderVo order,HttpServletResponse response){
			try {
				order.setHospitalId(order.getTeamId());
			Map<String, Object> selectOrderList = iOrderSelectService.selectOrderList(order);
			List<OrderSelectInfoEntity> list = (List<OrderSelectInfoEntity>)selectOrderList.get("list");
				String finalFileName = URLEncoder.encode("订单列表"+TimeUtils.getTime(TimeType.yyyyMMddHHmmssSSS)+".xls", "UTF8");
				//response.addHeader("Content-Disposition", "attachment;filename=" + finalFileName);
				response.setContentType("application/force-download");
				response.addHeader("Content-Disposition", "attachment;filename=" + finalFileName);
				OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
				ExcelUtil.getInstance().exportObj2Excel(toClient, list, OrderSelectInfoEntity.class);
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
	@RequestMapping(value = "listforExcel")
	//查询订单列表
		public List<OrderSelectInfoEntity> listforExcel(@RequestBody SelectOrderVo order){
		order.setHospitalId(order.getTeamId());
			Map<String, Object> selectOrderList = iOrderSelectService.selectOrderList(order);
			List<OrderSelectInfoEntity> list = (List<OrderSelectInfoEntity>)selectOrderList.get("list");
			return list;
			
		}
	//查询订单详情
	@RequestMapping(value = "query")
	public SysResult selectOrderInfo(@RequestBody SelectOrderVo order){
		if(order==null){
			return SysResult.error("参数不能为空");
		}
		OrderInfoDto selectOrderInfo = iOrderSelectService.selectOrderInfo(order);
		
		return SysResult.ok(selectOrderInfo);
		
	}
}
